package com.chookss.home.search.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.logContacts.ContactsActivity;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.PermissionUtils;
import com.chookss.tools.Utils;
import com.chookss.view.CircleImageView;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

public class SearchAddressbookAdapter extends BaseQuickAdapter<SearchAllEntity.UserBean.ListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String key;
    private String phoneFlag="1";//0禁止通话1正常
    public SearchAddressbookAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.UserBean.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        phoneFlag=new ShareUtils().getString(context,StaticClass.PHONE_FLAG,"1");
    }

    @Override
    protected void convert(BaseViewHolderEx helper, SearchAllEntity.UserBean.ListBean item) {
        TextView tvTiltle = helper.getView(R.id.tvSearchAddressbookTitle);
        TextView tvSearchType = helper.getView(R.id.tvSearchAddressbookContains);
        LinearLayout llSearchAddressbookPhone = helper.getView(R.id.llSearchAddressbookPhone);
        CircleImageView ivSearchAddressbook = helper.getView(R.id.ivSearchAddressbook);
        ImageView ivPhone = helper.getView(R.id.ivPhone);
        if ("0".equals(item.getType())) {
            //部门
            ivSearchAddressbook.setVisibility(View.GONE);
            llSearchAddressbookPhone.setVisibility(View.GONE);
            tvTiltle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getEmployeeName() + "(" + item.getEmpNum() + ")", key));
            tvSearchType.setText(item.getBelgName());
        } else {
            //人
            ivSearchAddressbook.setVisibility(View.VISIBLE);
            llSearchAddressbookPhone.setVisibility(View.VISIBLE);
            GlideUtils.loadAvatar(context, item.getHeadPhotoThumbUrl(), ivSearchAddressbook);
            tvTiltle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getEmployeeName(), key));
            tvSearchType.setText(item.getPost());
        }
        if("0".equals(phoneFlag)){
            ivPhone.setAlpha(0.3f);
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("0".equals(item.getType())) {
                    //是组织
                    String companyNmae = new ShareUtils().getString(context, StaticClass.COMPANYNAME, "");
                    if (Utils.isNull(companyNmae)) {
                        companyNmae = context.getResources().getString(R.string.app_name);
                    }
                    ArrayList<String> nameList = new ArrayList<>();//组织及部门排列
                    nameList.add(companyNmae);
                    Intent intent = new Intent(context, ContactsActivity.class);
                    intent.putExtra("orgCode", item.getEmployeeCode());
                    intent.putExtra("companyNmae", companyNmae);
                    intent.putExtra("oldCompanyNmae", item.getEmployeeName());
                    intent.putStringArrayListExtra("nameList", nameList);
                    intent.putExtra("department", item.getEmployeeName());
                    context.startActivity(intent);
                }else{
                    if("1".equals(phoneFlag)){
                        toPhone(item.getPhone());
                    }else{
                        MyToast.show("管理员已禁用拨打电话功能");
                    }

                }
            }
        });
        llSearchAddressbookPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("1".equals(phoneFlag)){
                    toPhone(item.getPhone());
                }else{
                    MyToast.show("管理员已禁用拨打电话功能");
                }
            }
        });
        ivSearchAddressbook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = new Intent(context, PersonalHomeActivity.class);
                intent.putExtra("employeeCode", item.getEmployeeCode());
                context.startActivity(intent);
            }
        });
    }

    private void toPhone(String phone) {
        if (!Utils.isNull(phone)) {
            XXPermissions.with((Activity) context)
                    .permission(Permission.CALL_PHONE)
                    .request(new OnPermissionCallback() {

                        @Override
                        public void onGranted(List<String> granted, boolean all) {
                            if (all) {
                                Intent callIntent = new Intent(Intent.ACTION_CALL,
                                        Uri.parse("tel:" + phone.replace("-", "")));
                                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                    return;
                                }
                                context.startActivity(callIntent);
                            } else {
                                PermissionUtils.errorSinglePermission(granted);
                            }
                        }

                        @Override
                        public void onDenied(List<String> denied, boolean quick) {
                            PermissionUtils.errorPermission(context, denied, quick);
                        }
                    });
        }
    }

    public void setKey(String key) {
        this.key = key;
    }
}
